package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.LogicTable;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyUnionInfoDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYDissolveUnionReq;
import com.game.server.message.proto.out.XYDissolveUnionRsp;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.system.AbstractRoom;
import com.game.server.system.AbstractTable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class XYDissolveUnionReq extends AbstractXYDissolveUnionReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            XYDissolveUnionRsp rsp = new XYDissolveUnionRsp();
            rsp.setIsok(1);
            if (client.getClubid() == this.clubid) {
                XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
                if (xyClubInfo!=null && xyClubInfo.getCreatorid() == client.getUserId()){
                    XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(xyClubInfo.getUnionid());
                    if (unionInfo!=null && xyClubInfo.getUnionid()>0 && unionInfo.getCreator_clubid()==xyClubInfo.getId()){
                        rsp.setIsok(2);
                        boolean nextFlg = true;
                        Collection<AbstractRoom> rooms = LogicServer.getInstance().getRooms();
                        for (AbstractRoom room : rooms) {
                            ArrayList<AbstractTable> tables = room.getTables();
                            for (AbstractTable tempTable : tables) {
                                LogicTable tab = (LogicTable) tempTable;
                                if (tab.getUnionid() == unionInfo.getId()){
                                    nextFlg = false;
                                    break;
                                }
                            }
                        }
                        if (nextFlg){

                            XYInformDataAlterRsp alterRsp = new XYInformDataAlterRsp();
                            alterRsp.setType(2);
                            alterRsp.setOperateclubid(this.clubid);
                            alterRsp.setClubid(this.clubid);
                            alterRsp.setUnionid(unionInfo.getId());
                            unionInfo.broadCastAdminPlayer(alterRsp);

                            XyUnionInfoDao.del(unionInfo.getId());
                            List<Integer> clubids = unionInfo.getClubids();
                            for (Integer integer : clubids) {
                                XyClubInfo removeClub = DataManager.getInstance().getMapClub().get(integer);
                                XyClubInfoDao.updateUnionId(removeClub.getId(),0);
                                removeClub.setUnionid(0);
                            }
                            DataManager.getInstance().getMapUnion().remove(Integer.valueOf(unionInfo.getId()));
                            rsp.setIsok(0);
                        }
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
